## An Experimental Analysis of the Prize–Probability Tradeoff in Stopping Problems
# Yair Antler and Ayala Arad (Tel Aviv University)
# 4.1 Alternative theory-based explanations: 
# Individual student parameter estimation  

# Clear Global Environment and set working directory
rm(list=ls()) 

# Set working directory 
setwd("YOUR_FOLDER_PATH/stopping_rules_replication_folder")

## Package Install (install prior to load)
# install.packages("tidyverse")
# install.packages("lubridate")
# install.packages("rio")
# install.packages("bbmle")

# Load Packages 
library(tidyverse)
library(lubridate)
library(rio)
library(bbmle)

# loading function library 
source("Replication_scripts/stopping_rules_func_library/MaxLike_func_lib_script.R")

# Load Data
df <- import("Data/stopping_rules_data.csv")
data_regret <- import("Data/regret_numbers.csv")

#________________________________________________________________________________________________________________________________________________#

## Stopping rules individual estimates 

# Two-Stage Qualitative Tradeoff Resolution - 2S-QTR Model
indiv_2sQTR_estim_full <- MaxLikeSR(data=df, sub_sample="full", util_fun="2sQTR", init=list(epsilon=0.4, alpha=0.2, beta=0.4), bootstrapping = FALSE)

# BOOTSTRAPPING: Two-Stage Qualitative Tradeoff Resolution - 2S-QTR Model
indiv_2sQTR_BOOTSTRAP_estim_full <- MaxLikeSR(data=df, sub_sample="full", util_fun="2sQTR",
                                              init=list(epsilon=0.4, alpha=0.2, beta=0.4), bootstrapping = TRUE)

# Constant Relative Risk Aversion (CRRA)
indiv_CRRA_estim_full <- MaxLikeSR(data=df, sub_sample="full", util_fun="CRRA", init=list(CRRA=0.8))

# Cumulative Prospect Theory (3 parameter model)
indiv_CPT_3param_estim_full <- MaxLikeSR(data=df, sub_sample="full", util_fun="CPT_3param", init=list(loss=2, delta=0.4, alpha=0.4))

# Cumulative Prospect Theory (5 parameter model)
indiv_CPT_5param_estim_full <- MaxLikeSR(data=df, sub_sample="full", util_fun="CPT_5param",
                                         init=list(loss=2, alpha = 0.4, beta = 0.4, gamma = 0.6, delta = 0.6))

# Disappointment Aversion 
indiv_disapAver_estim_full  <- MaxLikeSR(data=df, sub_sample="full", util_fun="disapAver", init=list(beta=2))

# Salience Theory
indiv_salience_estim_full <- MaxLikeSR(data=df, sub_sample="full", util_fun="salience", init=list(delta=0.4))

# Regret Aversion 
indiv_regretAver_estim_full <- MaxLikeSR(data=df, sub_sample="full", util_fun="regretAver",
                                         init=list(alpha=1, beta=2, sigma=0.4), df_regret_probs = data_regret)

# Rank Dependent Utility Prelec
indiv_RDU_prelec_estim_full <- MaxLikeSR(data=df, sub_sample="full", util_fun="RDU_prelec", init=list(RDU=1.5, b=1.5, alpha=0.6, loss = 2))

# Rank Dependent Utility Goldstein and Einhorn
indiv_RDU_GE_estim_full <- MaxLikeSR(data=df, sub_sample="full", util_fun="RDU_GE", init=list(b=1.1, beta=1.1, alpha=0.6, loss = 2))

# Rank Dependent Utility Kahneman and Tversky 
indiv_RDU_KT_estim_full <- MaxLikeSR(data=df, sub_sample="full", util_fun="RDU_KT", init=list(delta=0.6, alpha=0.6, loss=2))

# exporting results to excel
export(list(TwosQTR =      indiv_2sQTR_estim_full,
            BOOT_TwosQTR = indiv_2sQTR_BOOTSTRAP_estim_full,
            CRRA =         indiv_CRRA_estim_full,
            CPT_3param =   indiv_CPT_3param_estim_full,
            CPT_5param =   indiv_CPT_5param_estim_full,
            disapAver =    indiv_disapAver_estim_full,
            salience =     indiv_salience_estim_full,
            regretAver =   indiv_regretAver_estim_full,
            RDU_prelec =   indiv_RDU_prelec_estim_full,
            RDU_GE =       indiv_RDU_GE_estim_full,
            RDU_KT =       indiv_RDU_KT_estim_full),
       "Stopping_rules_indiv_estimation_results_summary.xlsx")



